package com.lanchetech.user.controller;

import com.lanchetech.bean.request.BossInviteShopReq;
import com.lanchetech.bean.request.PageReq;
import com.lanchetech.bean.request.ShopAnnouncementPageReq;
import com.lanchetech.bean.request.SpuPageReq;
import com.lanchetech.bean.response.BasePageResp;
import com.lanchetech.bean.response.BaseResp;
import com.lanchetech.bean.response.ResultData;
import com.lanchetech.bean.response.SpuResp;
import com.lanchetech.bean.vo.*;
import com.lanchetech.entity.*;
import com.lanchetech.user.config.ReturnPayInfoVO;
import com.lanchetech.user.service.ShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


@Api(value = "门店", tags = "门店")
@RequestMapping("/api/shop")
@RestController
public class ShopController {

    @Autowired
    ShopService shopService;


    @ApiOperation("获取附近的门店分页")
    @PostMapping("/getNearbyShopPage")
    public ResultData<BasePageResp<Shop>> getNearbyShopPage(@RequestBody PageReq req) {
        return shopService.getNearbyShopPage(req);
    }


    @ApiOperation("获取门店详情页")
    @GetMapping("/getShopDetail")
    public ResultData<ShopDetailVO> getShopDetail(@AuthenticationPrincipal User user, @RequestParam("id") Long id) {
        return shopService.getShopDetail(user, id);
    }


    @ApiOperation("获取门店简单详情")
    @GetMapping("/getShop")
    public ResultData<Shop> getShop(@RequestParam("id") Long id) {
        return shopService.getShop(id);
    }


    @ApiOperation("编辑门店预约设置")
    @PostMapping("/editAppointmentSetting")
    public BaseResp editAppointmentSetting(@RequestBody AppointmentSetting req) {
        return shopService.editAppointmentSetting(req);
    }


    @ApiOperation("获取预约列表分页")
    @PostMapping("/getAppointmentPage")
    public ResultData<BasePageResp<AppointmentVO>> getAppointmentPage(@RequestBody PageReq req) {
        return shopService.getAppointmentPage(req);
    }

    @ApiOperation("获取某天的预约信息")
    @GetMapping("/getAppointmentByDay")
    public ResultData<AppointmentByDayVO> getAppointmentByDay(@RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date date, @RequestParam("appointmentSettingId") Long appointmentSettingId) {
        return shopService.getAppointmentByDay(date, appointmentSettingId);
    }

    @ApiOperation("门店预约下单")
    @PostMapping("/editAppointment")
    public ResultData<ReturnPayInfoVO> editAppointment(@AuthenticationPrincipal User user, @RequestBody Appointment req, HttpServletRequest httpServletRequest) {
        return shopService.editAppointment(user, req, httpServletRequest);
    }

    @ApiOperation(value = "门店预约下单微信支付成功后，处理订单状态【微信支付回调】")
    @RequestMapping(value = "/appointmentOrderNotify", method = {RequestMethod.GET, RequestMethod.POST})
    public String appointmentOrderNotify(@RequestBody String xmlData) {
        return shopService.appointmentOrderNotify(xmlData);
    }

    @ApiOperation("获取门店预约设置分页")
    @PostMapping("/getAppointmentSettingPage")
    public ResultData<BasePageResp<AppointmentSettingVO>> getAppointmentSettingPage(@AuthenticationPrincipal User user, @RequestBody PageReq req) {
        return shopService.getAppointmentSettingPage(user, req);
    }

    @ApiOperation("取消预约")
    @GetMapping("/cancelAppointment")
    public BaseResp cancelAppointment(@RequestParam("id") Long id) {
        return shopService.cancelAppointment(id);
    }

    @ApiOperation(value = "主动取消预约的退款处理回调【微信支付退款回调】")
    @RequestMapping(value = "/appointmentRefundNotify", method = {RequestMethod.GET, RequestMethod.POST})
    public String appointmentRefundNotify(@RequestBody String xmlData) {
        return shopService.appointmentRefundNotify(xmlData);
    }

    @ApiOperation("商店核销时获取订单信息")
    @GetMapping("/getAppointmentByHashId")
    @PreAuthorize("hasRole('ROLE_CLERK')")
    public ResultData<AppointmentVO> getAppointmentByHashId(@RequestParam("hashId") String hashId) {
        return shopService.getAppointmentByHashId(hashId);
    }

    @ApiOperation("获取预约详情")
    @GetMapping("/getAppointmentDetail")
    public ResultData<AppointmentVO> getAppointmentDetail(@RequestParam("id") Long id) {
        return shopService.getAppointmentDetail(id);
    }

    @ApiOperation("商店确认核销订单商品")
    @GetMapping("/verifyConfirmAppointment")
    @PreAuthorize("hasRole('ROLE_CLERK')")
    public BaseResp verifyConfirmAppointment(@RequestParam("hashId") String hashId) {
        return shopService.verifyConfirmAppointment(hashId);
    }



    @ApiOperation("编辑活动报名")
    @PostMapping("/editActivityRegister")
    public BaseResp editActivityRegister(@RequestBody ActivityRegister activityRegister) {
        return shopService.editActivityRegister(activityRegister);
    }

    @ApiOperation("删除活动报名")
    @GetMapping("/deleteActivityRegister")
    public BaseResp deleteActivityRegister(@RequestParam("id") Long id) {
        return shopService.deleteActivityRegister(id);
    }

    @ApiOperation("获取活动报名分页")
    @PostMapping("/getActivityRegisterPage")
    public ResultData<BasePageResp<ActivityRegisterVO>> getActivityRegisterPage(@RequestBody PageReq req) {
        return shopService.getActivityRegisterPage(req);
    }

    @ApiOperation("获取活动报名详情")
    @GetMapping("/getActivityRegisterDetail")
    public ResultData<ActivityRegister> getActivityRegisterDetail(@RequestParam("id") Long id) {
        return shopService.getActivityRegisterDetail(id);
    }

    @ApiOperation("获取行业类目一级列表")
    @GetMapping("/getIndustryFirstList")
    public ResultData<List<IndustryIdAndNameVO>> getIndustryFirstList(@AuthenticationPrincipal User user) {
        return shopService.getIndustryFirstList(user);
    }

    @ApiOperation("获取行业类目二级列表")
    @PostMapping("/getIndustrySecondList")
    public ResultData<List<IndustryIdAndNameVO>> getIndustrySecondList(@RequestBody PageReq req) {
        return shopService.getIndustrySecondList(req);
    }

    @ApiOperation("获取某商品在店铺近六天的预约情况")
    @GetMapping("/getSpuAppointmentStatistics")
    public ResultData<List<StatisticsVO>> getSpuAppointmentStatistics(@RequestParam("spuId") Long spuId) {
        return shopService.getSpuAppointmentStatistics(spuId);
    }

    @ApiOperation("获取我推荐的商家分页")
    @GetMapping("/getMyRecommendShopPage")
    public ResultData<BasePageResp<Shop>> getMyRecommendShopPage(@RequestBody PageReq req) {
        return shopService.getMyRecommendShopPage(req);
    }


    @ApiOperation("获取商家客服信息")
    @GetMapping("/getStoreMsgUser")
    public ResultData<MsgUser> getStoreMsgUser(@RequestParam("shopId") Long shopId, @RequestParam("msgUserId") Long msgUserId) {
        return shopService.getStoreMsgUser(shopId, msgUserId);
    }

    @ApiOperation("获取老板商店分页")
    @PostMapping("/getBossShopPage")
    public ResultData<BasePageResp<BossVO>> getBossShopPage(@RequestBody PageReq req) {
        return shopService.getBossShopPage(req);
    }

    @ApiOperation("切换店铺")
    @GetMapping("/switchShop")
    public BaseResp switchShop(@RequestParam("shopId") Long shopId) {
        return shopService.switchShop(shopId);
    }

    @ApiOperation("获取商户通知分页")
    @PostMapping("/getShopAnnouncementPage")
    public ResultData<BasePageResp<ShopAnnouncementVO>> getShopAnnouncementPage(@RequestBody ShopAnnouncementPageReq req) {
        return shopService.getShopAnnouncementPage(req);
    }

    @ApiOperation("获取通知记录详情")
    @GetMapping("/getAnnouncementDetail")
    public ResultData<ShopAnnouncementVO> getShopAnnouncementDetail(@RequestParam("id") Long id) {
        return shopService.getShopAnnouncementDetail(id);
    }

    @ApiOperation("获取我推荐的店铺分页")
    @PostMapping("getBossInviteShopPage")
    public ResultData<BasePageResp<BossInviteShopVO>> getBossInviteShopPage(@RequestBody BossInviteShopReq req) {
        return shopService.getBossInviteShopPage(req);
    }

    @ApiOperation("获取老板推荐统计")
    @GetMapping("getBossInviteStatistics")
    public ResultData<BossInviteStatisticsVO> getBossInviteStatistics(@AuthenticationPrincipal User user) {
        return shopService.getBossInviteStatistics(user);
    }

    @ApiOperation("获取老板推荐店铺列表")
    @GetMapping("getBossInviteShopList")
    public ResultData<List<BossInviteShopVO>> getBossInviteShopList(@AuthenticationPrincipal User user) {
        return shopService.getBossInviteShopList(user);
    }

    @ApiOperation("获取老板推荐佣金列表")
    @GetMapping("getBossInviteCommissionList")
    public ResultData<List<UserCommissionVO>> getBossInviteCommissionList(@AuthenticationPrincipal User user) {
        return shopService.getBossInviteCommissionList(user);
    }

    @ApiOperation("获取店铺分页")
    @PostMapping("/getShopPage")
    public ResultData<BasePageResp<Shop>> getShopPage(@RequestBody PageReq req) {
        return shopService.getShopPage(req);
    }

    @ApiOperation("获取店铺首页商品分页")
    @PostMapping("/getSpuHomePage")
    public ResultData<BasePageResp<SpuResp>> getSpuHomePage(@RequestBody SpuPageReq req, @AuthenticationPrincipal User user){
        return shopService.getSpuHomePage(req, user);
    }

    @ApiOperation("获取店铺首页-活动榜商品分页")
    @PostMapping("/getSpuActivityRank")
    public ResultData<BasePageResp<FlashSaleSpuVO>> getSpuActivityRank(@RequestBody SpuPageReq req){
        return shopService.getSpuActivityRank(req);
    }

    /**
     * 获取店铺热搜词 目前获取商品最底层类目
     * @param shopId
     * @return
     */
    @ApiOperation("获取店铺热搜词")
    @GetMapping("/getHotWords")
    public ResultData<List<Category>> getHotWords(@RequestParam("shopId") Long shopId){
        return shopService.getHotWords(shopId);
    }
}
